﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the L, a, b values are filtered instead of R G B
    /// </summary>
    public class CIELabFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_LInterval = new DoubleInterval(0.0f, 100.0f);
        protected DoubleInterval m_aInterval = new DoubleInterval(0.0f, 10.0f);
        protected DoubleInterval m_bInterval = new DoubleInterval(0.0f, 10.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public CIELabFilter()
        {
        }

        /// <summary>
        /// Specify the interval
        /// </summary>
        /// <param name="_l"></param>
        /// <param name="_a"></param>
        /// <param name="_b"></param>
        public CIELabFilter(DoubleInterval _l, DoubleInterval _a, DoubleInterval _b)
        {
            m_LInterval = _l;
            m_aInterval = _a;
            m_bInterval = _b;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_cieLabFilter"></param>
        internal CIELabFilter(CIELabFilter _cieLabFilter)
        {
            this.m_LInterval = _cieLabFilter.m_LInterval;
            this.m_aInterval = _cieLabFilter.m_aInterval;
            this.m_bInterval = _cieLabFilter.m_bInterval;
        }

        /// <summary>
        /// Gets or sets l
        /// </summary>
        public DoubleInterval L
        {
            get { return m_LInterval; }
            set { m_LInterval = value; }
        }

        /// <summary>
        /// Gets or sets a
        /// </summary>
        public DoubleInterval a
        {
            get { return m_aInterval; }
            set { m_aInterval = value; }
        }

        /// <summary>
        /// Gets or sets the b.
        /// </summary>
        /// <value>The b.</value>
        public DoubleInterval b
        {
            get { return m_bInterval; }
            set { m_bInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            CIELab cielab = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    cielab = new CIELab(bitmap.GetPixel(x,y));

                    if (m_LInterval.Contains(cielab.L)
                        && m_aInterval.Contains(cielab.a)
                        && m_bInterval.Contains(cielab.b))
                    {
                        bitmap.SetPixel(x, y, cielab.ToColor());
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CIELabFilter(this);
        }
    }
}
